const copywebpackplugin=require('copy-webpack-plugin')
const entry=require('./webpack_config/entry_webpack.js')
const glob = require('glob');
const purifyCSSPlugin = require('purifycss-webpack')
const htmlPlugin = require('html-webpack-plugin');
const path = require('path');
const uglify = require('uglifyjs-webpack-plugin');
const extractTextPlugin = require('extract-text-webpack-plugin');
const webpack=require('webpack');
console.log(encodeURIComponent(process.env.type));
if (process.env.type == "build") {
   
    var website = {
        publicPath: 'http://jspang.com:1112/'
    }
} else {
    

    var website = {
        publicPath: 'http://localhost:1112/'
    };
}





module.exports = {
    devtool: 'eval-source-map',//4种模式：1、source-map/ 打包最慢、最详细、生成独立文件，能跟打包后的文件很好的结合，既包括 行 又包括 列，但是打包最慢 2、cheap-module-sowurce-map生成独立文件，只包括 行 不包括 列，生成独立文件 3、eval-source-map不生成独立文件 快 执行时会有性能和安全性的隐患，一定是在开发环境，上线前一定删除eval-source-map 4、cheap-module-source-map 适合小项目 列 

    entry:
    {
        entry:'./src/entry.js',//entry.path
        jquery:'jquery',
        vue:'vue'

    },

    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
        publicPath: website.publicPath
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: extractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        { loader: 'css-loader', options: { importLoaders: 1 } },
                        // 'postcss-loader'
                        {
                            loader: 'postcss-loader', options: {
                                config: {
                                    path: 'postcss.config.js'
                                }
                            }
                        }
                    ]


                })

            },
            {
                test: /\.(npg|jpg|gif)/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 5000,
                            outputPath: "images/"
                        }
                    }
                ]
            },
            {
                test: /\.(htm|html)$/i,
                use: ['html-withimg-loader']


            }, {
                test: /\.less$/,
                use: extractTextPlugin.extract({
                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "less-loader"
                    }],
                    fallback: "style-loader"

                })
            },
            {
                test: /\.scss$/,

                use: extractTextPlugin.extract({

                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "sass-loader"
                    }],

                    fallback: "style-loader"

                }),



            },
            {
                test: /\.(jsx|js)$/,
                use: {
                    loader: 'babel-loader',

                },
                exclude: /node-modules/
            }
        ]

    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name:['jquery','vue'],
            filename:'assets/js/[name].js',
            minChunks:2

        }),
        //new uglify(),
        new htmlPlugin({
            minify: { removeAttributequotes: true },
            hash: true,
            template: './src/index.html'
        }),
        new extractTextPlugin('./src/css/index.css'),
        new purifyCSSPlugin({
            paths: glob.sync(path.join(__dirname, 'src/*'))

        }),
        new webpack.ProvidePlugin({
            $:"jquery",//或JQuery:'jquery'
            

        }),
        new webpack.BannerPlugin('yue版权所有，看官方免费视频到。。。'),
        new copywebpackplugin([{
            from:__dirname+'/src/public',
            to:'./public'
        }])
        
    ],
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        host: 'localhost',
        compress: true,
        port: 1112
    },
    watchOptions:{
        poll:1000,//毫秒
        aggregateTimeout:500,//半秒内打包多次，以第一次为准
        ignored:/node-modules/,
    }

}